﻿<UserControl x:Class="Samba.Modules.CustomerModule.CustomerSelectorView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:FlexButton="clr-namespace:FlexButton;assembly=FlexButton"
     xmlns:VirtualKeyboard="clr-namespace:Samba.Presentation.Common.VirtualKeyboard;assembly=Samba.Presentation.Common"
     xmlns:Common="clr-namespace:Samba.Presentation.Common;assembly=Samba.Presentation.Common"
     xmlns:ListViewEx="clr-namespace:Samba.Presentation.Common.ListViewEx;assembly=Samba.Presentation.Common"
     xmlns:ListViewLM="clr-namespace:Samba.Presentation.Common.ListViewLM;assembly=Samba.Presentation.Common"
     mc:Ignorable="d" FontSize="20" d:DesignHeight="300" d:DesignWidth="300" xmlns:lex="http://schemas.root-project.org/xaml/presentation">
    <TabControl SelectedIndex="{Binding ActiveView}" BorderThickness="0" Padding="0">
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Visibility" Value="Collapsed" />
            </Style>
        </TabControl.ItemContainerStyle>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="9*" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <TabControl BorderThickness="0" Padding="0" Name="MainTabControl" Grid.Row="0" Grid.Column="0"
                 SelectedIndex="{Binding SelectedView}">
                <TabControl.ItemContainerStyle>
                    <Style TargetType="{x:Type TabItem}">
                        <Setter Property="Visibility" Value="Collapsed" />
                    </Style>
                </TabControl.ItemContainerStyle>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0" Margin="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <TextBlock Margin="3" Grid.Row="0" Grid.Column="0" FontSize="24" Text="{LocText PhoneNumber}" />
                        <TextBlock Margin="3" Grid.Row="0" Grid.Column="1" FontSize="24" Text="{LocText CustomerName}" />
                        <TextBlock Margin="3" Grid.Row="0" Grid.Column="2" FontSize="24" Text="{LocText Address}" />
                        <TextBlock Margin="3" Grid.Row="0" Grid.Column="3" FontSize="24" Text="{LocText Ticket}" />
                        <Common:FilteredTextBox Margin="3" Grid.Row="1" Grid.Column="0" FontSize="24" Name="PhoneNumber"
                             Type="Digits" Text="{Binding PhoneNumberSearchText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                             PreviewKeyDown="PhoneNumber_PreviewKeyDown" Loaded="PhoneNumber_Loaded" />
                        <TextBox Margin="3" Grid.Row="1" Grid.Column="1" FontSize="24" Name="CustomerName"
                             Text="{Binding CustomerNameSearchText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                             PreviewKeyDown="CustomerName_PreviewKeyDown" />
                        <TextBox Margin="3" Grid.Row="1" Grid.Column="2" FontSize="24" Name="Address" Text="{Binding AddressSearchText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                             PreviewKeyDown="Address_PreviewKeyDown" />
                        <TextBox Margin="3" Grid.Row="1" Grid.Column="3" FontSize="24" Name="TicketNo" Text="{Binding TicketSearchText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                             PreviewKeyDown="TicketNo_PreviewKeyDown" />
                    </Grid>
                    <ListView Grid.Row="1" Margin="5" Background="LightGray" VirtualizingStackPanel.IsVirtualizing="True"
                         IsSynchronizedWithCurrentItem="True" ListViewEx:ListViewSorter.IsListviewSortable="True"
                         ListViewLM:ListViewLayoutManager.Enabled="True" ItemsSource="{Binding FoundCustomers}"
                         SelectedItem="{Binding FocusedCustomer,Mode=TwoWay}" ItemContainerStyle="{StaticResource ListViewItemExContainerStyle}">
                        <ListView.View>
                            <GridView AllowsColumnReorder="False" ColumnHeaderContainerStyle="{StaticResource BlueBackground}"
                                 ColumnHeaderTemplate="{StaticResource WrappedHeaderTemplate}">
                                <GridViewColumn Header="{LocText CustomerName}"
                                     ListViewLM:ProportionalColumn.Width="1.5">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Name}" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="{LocText PhoneNumber}"
                                     ListViewLM:ProportionalColumn.Width="1">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding PhoneNumberText}" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="{LocText Address}"
                                     ListViewLM:ProportionalColumn.Width="1.3">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Address}" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="{LocText Note}"
                                     ListViewLM:ProportionalColumn.Width="1">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding Note}" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </Grid>
                <UniformGrid Columns="2">
                    <Grid Margin="5,5,20,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBlock Margin="0,0,2,2" Grid.Row="0" Grid.Column="0" Text="{LocText PhoneNumber}" />
                        <Common:MaskedTextBox Margin="0,0,2,2" Grid.Row="0" Grid.Column="1" Name="PhoneNumberTextBox"
                             InputMask="{Binding PhoneNumberInputMask}" UnmaskedText="{Binding SelectedCustomer.PhoneNumber,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                             PromptChar=" " PreviewKeyDown="PhoneNumberTextBox_PreviewKeyDown" />
                        <TextBlock Margin="0,0,2,2" Grid.Row="1" Grid.Column="0" Text="{LocText CustomerName}" />
                        <TextBox Margin="0,0,2,2" Grid.Row="1" Grid.Column="1" Text="{Binding SelectedCustomer.Name,UpdateSourceTrigger=PropertyChanged}" />
                        <TextBlock Margin="0,0,2,2" Grid.Row="2" Grid.Column="0" Text="{LocText GroupCode}" />
                        <ComboBox Margin="0,0,2,2" Grid.Row="2" Grid.Column="1" IsEditable="True" ItemsSource="{Binding SelectedCustomer.GroupCodes}" Text="{Binding SelectedCustomer.GroupCode,UpdateSourceTrigger=PropertyChanged}" />
                        <TextBlock Margin="0,0,2,2" Grid.Row="3" Grid.Column="0" Text="{LocText Address}" />
                        <TextBox Margin="0,0,2,2" Grid.Row="3" Grid.Column="1" Text="{Binding SelectedCustomer.Address,UpdateSourceTrigger=PropertyChanged}"
                             AcceptsReturn="True" />
                        <TextBlock Margin="0,0,2,2" Grid.Row="4" Grid.Column="0" Text="{LocText Note}" />
                        <TextBox Margin="0,0,2,2" Grid.Row="4" Grid.Column="1" Text="{Binding SelectedCustomer.Note,UpdateSourceTrigger=PropertyChanged}"
                             AcceptsReturn="True" />
                    </Grid>
                    <Grid Visibility="{Binding SelectedCustomer.IsNotNew,Converter={StaticResource VisibilityConverter}}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="auto" />
                        </Grid.RowDefinitions>
                        <UniformGrid Columns="2" Grid.Row="0">
                            <TextBlock FontSize="16" FontWeight="Bold" Text="{LocText TotalSales}"/>
                            <TextBlock FontSize="16" Text="{Binding SelectedCustomer.TotalTicketAmount,StringFormat='#,#0.00'}" />
                            <TextBlock FontSize="16" FontWeight="Bold" Text="{LocText AccountBalance}"/>
                            <TextBlock FontSize="16" Text="{Binding SelectedCustomer.AccountBalance,StringFormat='#,#0.00'}" />
                            <TextBlock FontSize="16" FontWeight="Bold" Text="{LocText LastOrderDate}"/>
                            <TextBlock FontSize="16" Text="{Binding SelectedCustomer.LastTicket.LastOrderDate,StringFormat='dd MMMM yyyy'}" />
                            <TextBlock FontSize="16" FontWeight="Bold" Text="{LocText LastOrderStatus}"/>
                            <TextBlock FontSize="16" Text="{Binding SelectedCustomer.LastTicketStateString}" />
                            <TextBlock Margin="0,5,0,0" FontSize="16" FontWeight="Bold" Text="{LocText LastOrder}"/>
                        </UniformGrid>
                        <ItemsControl Grid.Row="1" Margin="3" ItemsSource="{Binding SelectedCustomer.LastTicketLines}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="auto" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="auto" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Padding="1" FontSize="16" Grid.Column="0" Text="{Binding Quantity,StringFormat='#.##'}"
                                             Margin="3,0,6,0" />
                                        <TextBlock Padding="1" FontSize="16" Grid.Column="1" Text="{Binding Description}"
                                             Margin="0" />
                                        <TextBlock Padding="1" FontSize="16" Grid.Column="2" Text="{Binding TotalPrice, StringFormat='#,#0.00'}"
                                             Margin="0" HorizontalAlignment="Right" />
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <Grid Grid.Row="2" Margin="3">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock FontSize="16" FontWeight="Bold" Grid.Column="0" HorizontalAlignment="Stretch"
                                 Text="{LocText Total}" />
                            <TextBlock FontSize="16" FontWeight="Bold" Grid.Column="1" HorizontalAlignment="Right"
                                 Text="{Binding SelectedCustomer.TicketTotal,StringFormat='#,#0.00'}" />
                        </Grid>
                    </Grid>
                </UniformGrid>
            </TabControl>
            <Viewbox Grid.Row="1">
                <VirtualKeyboard:KeyboardView>
                </VirtualKeyboard:KeyboardView>
            </Viewbox>
            <Grid Grid.Column="1" Grid.RowSpan="2" Background="Silver">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0">
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{LocText Close}"
                         ButtonColor="Red" Command="{Binding CloseScreenCommand}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{LocText Clear}"
                         Click="FlexButton_Click" Visibility="{Binding IsClearVisible,Converter={StaticResource VisibilityConverter}}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Command="{Binding ResetCustomerCommand}"
                         Content="{Binding ResetCustomerCommand.Caption}" Visibility="{Binding IsResetCustomerVisible,Converter={StaticResource VisibilityConverter}}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding SelectCustomerCommand.Caption}"
                         Command="{Binding SelectCustomerCommand}" Name="SelectCustomerButton" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding MakePaymentCommand.Caption}"
                         Command="{Binding MakePaymentCommand}" Visibility="{Binding IsMakePaymentVisible,Converter={StaticResource VisibilityConverter}}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding CreateCustomerCommand.Caption}"
                         Command="{Binding CreateCustomerCommand}" Name="CreateCustomerButton" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding FindTicketCommand.Caption}"
                         Command="{Binding FindTicketCommand}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding DisplayCustomerAccountCommand.Caption}"
                         Command="{Binding DisplayCustomerAccountCommand}" />
                </StackPanel>
            </Grid>
        </Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="9*" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Left">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>
                <TextBlock FontWeight="Bold" Margin="5" Grid.Row="0" Grid.Column="0" Text="{LocText CustomerName}">
                </TextBlock>
                <TextBlock Margin="5" Grid.Row="0" Grid.Column="1" Text="{Binding SelectedCustomer.Name}" />
                <TextBlock FontWeight="Bold" Margin="5" Grid.Row="0" Grid.Column="2" Text="{LocText PhoneNumber}">
                </TextBlock>
                <TextBlock Margin="5" Grid.Row="0" Grid.Column="3" Text="{Binding SelectedCustomer.PhoneNumberText}" />
                <TextBlock FontWeight="Bold" Margin="5" Grid.Row="1" Grid.Column="0" Text="{LocText Address}">
                </TextBlock>
                <TextBlock Margin="5" Grid.Row="1" Grid.Column="1" Text="{Binding SelectedCustomer.Address}" />
                <TextBlock FontWeight="Bold" Margin="5" Grid.Row="1" Grid.Column="2" Text="{LocText Note}">
                </TextBlock>
                <TextBlock Margin="5" Grid.Row="1" Grid.Column="3" Text="{Binding SelectedCustomer.Note}" />
            </Grid>
            <ListView BorderThickness="0" Grid.Row="1" Grid.Column="0" Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
                 VirtualizingStackPanel.IsVirtualizing="True" IsSynchronizedWithCurrentItem="True"
                 ListViewLM:ListViewLayoutManager.Enabled="True" ItemsSource="{Binding SelectedCustomerTransactions}"
                 ItemContainerStyle="{StaticResource ListViewItemExContainerStyle}">
                <ListView.View>
                    <GridView AllowsColumnReorder="False" ColumnHeaderContainerStyle="{StaticResource BlueBackground}"
                         ColumnHeaderTemplate="{StaticResource WrappedHeaderTemplate}">
                        <GridViewColumn Header="{LocText Date}"
                             ListViewLM:ProportionalColumn.Width="1.7">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock TextTrimming="CharacterEllipsis" FontSize="18" Text="{Binding Date}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="{LocText Description}"
                             ListViewLM:ProportionalColumn.Width="3">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock TextTrimming="CharacterEllipsis" FontSize="18" Text="{Binding Description}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="{LocText Liability}"
                             ListViewLM:ProportionalColumn.Width="1">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="18" Foreground="{Binding TextColor}" Text="{Binding Receivable,StringFormat='#,#0.00;-#,#0.00; '}"
                                         TextAlignment="Right" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="{LocText Receiveable}"
                             ListViewLM:ProportionalColumn.Width="1">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="18" Foreground="{Binding TextColor}" Text="{Binding Liability,StringFormat='#,#0.00;-#,#0.00; '}"
                                         TextAlignment="Right" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="{LocText GrandTotal}"
                             ListViewLM:ProportionalColumn.Width="1">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="18" Foreground="{Binding TextColor}" Text="{Binding Balance,StringFormat='#,#0.00;-#,#0.00; '}"
                                         TextAlignment="Right" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
            <ListView Grid.Row="2" Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
                             ListViewLM:ListViewLayoutManager.Enabled="True" BorderThickness="0" ItemContainerStyle="{StaticResource ListViewItemExContainerStyle}">
                <ListView.View>
                    <GridView AllowsColumnReorder="False" ColumnHeaderContainerStyle="{StaticResource SilverBackground}"
                                     ColumnHeaderTemplate="{StaticResource WrappedFooterTemplate}">
                        <GridViewColumn ListViewLM:ProportionalColumn.Width="4.7" />
                        <GridViewColumn Header="{Binding TotalReceivable}" ListViewLM:ProportionalColumn.Width="1" />
                        <GridViewColumn Header="{Binding TotalLiability}" ListViewLM:ProportionalColumn.Width="1" />
                        <GridViewColumn Header="{Binding TotalBalance}" ListViewLM:ProportionalColumn.Width="1" />
                    </GridView>
                </ListView.View>
            </ListView>
            <Grid Grid.Column="1" Grid.RowSpan="2" Background="Silver">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0">
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{LocText Close}"
                         ButtonColor="Red" Command="{Binding CloseAccountScreenCommand}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding AddReceivableCommand.Caption}"
                         Command="{Binding AddReceivableCommand}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding AddLiabilityCommand.Caption}"
                         Command="{Binding AddLiabilityCommand}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding GetPaymentFromCustomerCommand.Caption}"
                         Command="{Binding GetPaymentFromCustomerCommand}" />
                    <FlexButton:FlexButton Margin="2" Height="60" Content="{Binding MakePaymentToCustomerCommand.Caption}"
                         Command="{Binding MakePaymentToCustomerCommand}" />
                </StackPanel>
            </Grid>
        </Grid>
    </TabControl>
</UserControl>